<?php
declare(strict_types=1);

use think\migration\Migrator;
use think\migration\db\Column;

class CreateMessageConfig extends Migrator
{
    public function up()
    {
        // 创建消息配置表
        $this->table('message_config', ['engine' => 'InnoDB', 'comment' => '消息配置表'])
            ->addColumn('merchant_id', 'integer', ['null' => false, 'default' => 0, 'comment' => '商家ID,0为平台配置'])
            ->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'comment' => '消息类型:sms短信,mp公众号,mini小程序'])
            ->addColumn('provider', 'string', ['limit' => 20, 'default' => '', 'comment' => '服务商:aliyun,tencent等'])
            ->addColumn('app_id', 'string', ['limit' => 100, 'default' => '', 'comment' => 'AppID'])
            ->addColumn('app_secret', 'string', ['limit' => 100, 'default' => '', 'comment' => 'AppSecret'])
            ->addColumn('sign_name', 'string', ['limit' => 50, 'default' => '', 'comment' => '签名名称'])
            ->addColumn('status', 'boolean', ['null' => false, 'default' => 1, 'comment' => '状态 0禁用 1启用'])
            ->addColumn('create_time', 'datetime', ['null' => false, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => false, 'comment' => '更新时间'])
            ->addIndex(['merchant_id', 'type'], ['unique' => true])
            ->create();

        // 创建消息模板表
        $this->table('message_template', ['engine' => 'InnoDB', 'comment' => '消息模板表'])
            ->addColumn('merchant_id', 'integer', ['null' => false, 'default' => 0, 'comment' => '商家ID,0为平台配置'])
            ->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'comment' => '消息类型:sms短信,mp公众号,mini小程序'])
            ->addColumn('scene', 'string', ['limit' => 50, 'null' => false, 'comment' => '场景标识'])
            ->addColumn('template_code', 'string', ['limit' => 100, 'null' => false, 'comment' => '模板CODE'])
            ->addColumn('template_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '模板名称'])
            ->addColumn('template_content', 'string', ['limit' => 500, 'null' => false, 'comment' => '模板内容'])
            ->addColumn('template_params', 'string', ['limit' => 500, 'null' => false, 'comment' => '模板参数'])
            ->addColumn('status', 'boolean', ['null' => false, 'default' => 1, 'comment' => '状态 0禁用 1启用'])
            ->addColumn('create_time', 'datetime', ['null' => false, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => false, 'comment' => '更新时间'])
            ->addIndex(['merchant_id', 'type', 'scene'], ['unique' => true])
            ->create();

        // 初始化平台消息配置
        $configs = [
            [
                'merchant_id' => 0,
                'type' => 'sms',
                'provider' => 'aliyun',
                'sign_name' => '裹裹大师',
                'app_id' => '',
                'app_secret' => '',
                'status' => 1
            ],
            [
                'merchant_id' => 0,
                'type' => 'mp',
                'provider' => 'wechat',
                'app_id' => '',
                'app_secret' => '',
                'status' => 1
            ],
            [
                'merchant_id' => 0,
                'type' => 'mini',
                'provider' => 'wechat',
                'app_id' => '',
                'app_secret' => '',
                'status' => 1
            ]
        ];

        foreach ($configs as &$config) {
            $config['create_time'] = date('Y-m-d H:i:s');
            $config['update_time'] = date('Y-m-d H:i:s');
        }

        $this->table('message_config')->insert($configs)->save();

        // 初始化平台消息模板
        $templates = [
            // ... 之前的模板配置保持不变 ...
        ];

        foreach ($templates as &$template) {
            $template['create_time'] = date('Y-m-d H:i:s');
            $template['update_time'] = date('Y-m-d H:i:s');
        }

        $this->table('message_template')->insert($templates)->save();
    }

    public function down()
    {
        $this->dropTable('message_template');
        $this->dropTable('message_config');
    }
} 